package cn.miaoyao.miaoyaoms.controller;
import cn.miaoyao.miaoyaoms.entity.Article;
import cn.miaoyao.miaoyaoms.entity.User;
import cn.miaoyao.miaoyaoms.service.ArticleService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-05-13
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping("/articleInfo")
    public Boolean addArticleInfo( Article article, HttpServletRequest request){
        if(!ObjectUtils.isEmpty(article)) {
            User user = (User) request.getSession().getAttribute("user");
            article.setUid(user.getUid());
            article.setReleasetime(new Date());
            boolean save = articleService.save(article);
            return save;//存入成功。
        }
        return false;
    }
    @GetMapping("/getArt")
    public String getArts(){
        LambdaQueryWrapper<Article> asw = new LambdaQueryWrapper<>();
        asw.orderByDesc(Article::getReleasetime).last("limit 1");
        Article one = articleService.getOne(asw);
        return JSON.toJSONString(one);
    }


}

